package com.gambi.tapestry.client;

import com.gambi.tapestry.client.gwtui.widgets.CheckBoxWidget;
import com.gambi.tapestry.client.gwtui.widgets.LoginFormWidget;
import com.gambi.tapestry.client.gwtui.widgets.LoginWidget;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class GWTUI extends GWTTapestry implements EntryPoint {

	@Override
	public void widgetFactory(TapestryGWTComponentInitializer component) {

		String id = component.getComponentId();
		GWT.log("ID = " + id);

		String type = component.getComponentType();
		GWT.log("Type = " + type);
		/*
		 * Add here the componentType IDs that this entry point can load
		 */
		Widget widget = null;
		String componentHostId = null;

		if ("CheckBox".equals(type)) {
			// Initialization of the widget here parameters here
			if (component.getComponentParameters().containsKey(
					"gwtServiceTarget")) {
				widget = new CheckBoxWidget(id, ((JSONString) component
						.getComponentParameters().get("div")).stringValue(),
						((JSONString) component.getComponentParameters().get(
								"gwtServiceTarget")).stringValue());
				// Set the host to be the id provided by tapestry
				componentHostId = id;
			} else {
				widget = new CheckBoxWidget(id, ((JSONString) component
						.getComponentParameters().get("div")).stringValue(),
						((JSONString) component.getComponentParameters().get(
								"eventLink")).stringValue(),
						toStringArray((JSONArray) component
								.getComponentParameters().get("eventList")));
				// Set the host to be the id provided by tapestry
				componentHostId = id;
			}
		} else if ("LoginWidget".equals(type)) {
			GWT.log("LoginWidget");
			String url = ((JSONString) component.getComponentParameters().get(
					"eventLink")).stringValue();
			GWT.log("URL " + url);
			url = url
					+ toStringArray((JSONArray) component
							.getComponentParameters().get("eventList"))[0];
			GWT.log("URL " + url);
			widget = new LoginWidget(id, ((JSONString) component
					.getComponentParameters().get("div")).stringValue(), url);
			componentHostId = id;

		} else if ("LoginFormWidget".equals(type)) {
			GWT.log("LoginFormWidget");
			String url = ((JSONString) component.getComponentParameters().get(
					"eventLink")).stringValue();
			GWT.log("URL " + url);
			url = url
					+ toStringArray((JSONArray) component
							.getComponentParameters().get("eventList"))[0];
			GWT.log("URL " + url);
			
			widget = new LoginFormWidget(id, url);
			componentHostId = id;
		}

		// Finally place the component
		if (widget != null && componentHostId != null) {
			RootPanel.get(componentHostId).add(widget);
		} else {
			GWT.log("Missing parameter: " + widget + ", " + componentHostId);
		}
	}

	private String[] toStringArray(JSONArray stringArray) {
		String[] result = new String[stringArray.size()];
		for (int i = 0; i < result.length; i++) {
			result[i] = ((JSONString) stringArray.get(i)).stringValue();
		}
		return result;
	}
}
